const express = require("express")
const {con,getMysql} = require('../../mysql/mysql.js'); 
const chapter = express.Router() //创建路由


//搜索chapter
chapter.get("/getallcharptersearch",async (req,res)=>{
    let {articleid,message}=req.query
    // console.log(1)
    let str=`select * from chapter where articleid=${articleid}  and chaptertitle like '%${message}%'`
    let data=await getMysql(str)
    // console.log(2)
    res.send({status:200,data:data})
})

//获取当前文章的全部点赞数
chapter.get('/allzan',async (req,res)=>{
    let {articleid}=req.query
    let str=`select * from dianzan where articleid=${articleid} and bool=1`
    let data=await getMysql(str)
    res.send(data)
})

//获取当前的章节的点赞状态
chapter.get("/getzanzt",async (req,res)=>{
    let {articleid,chapterid,readerid}=req.query
    // console.log(bool)
    let arr=[articleid,chapterid,readerid]
    let str1=`select * from dianzan where articleid=${articleid} and chapterid=${chapterid} and readerid=${readerid} and bool=1`
    let data1=await getMysql(str1)
    // console.log(data1)
    if(data1.length>0){
        res.send({status:200,message:"已经点赞了",bool:1})
    }else{
        res.send({status:202,message:"还没有点赞",bool:0})
    } 
})

//点赞接口,提交点赞
chapter.post("/chapterZan",async (req,res)=>{
    let {articleid,chapterid,readerid,bool}=req.body
    // console.log(bool)
    let arr=[articleid,chapterid,readerid,bool]
    let str1=`select * from dianzan where articleid=${articleid} and chapterid=${chapterid} and readerid=${readerid}`
    let data1=await getMysql(str1)
    // console.log(data1)
    if(data1.length>0){
        let str2="update dianzan set  bool=? where articleid=? and chapterid=? and readerid=?"
        let arr2=[bool,articleid,chapterid,readerid]
        let data=await getMysql(str2,arr2)
        res.send({status:200,message:"修改成功",data:data})
    }else{
        let str=`insert into dianzan (articleid,chapterid,readerid,bool) values (?,?,?,?)`
        let data=await getMysql(str,arr)
        res.send({status:200,message:"提交点赞成功",data:data})
    } 
})

//通过章节id获取，章节内容
chapter.get("/chapter",async (req,res)=>{
    let {chapterid}=req.query
    let str=`select * from chapter where chapterid=${chapterid}`
    let data=await getMysql(str)
    res.send({status:200,message:"获取成功",data:data})
})

//根据id删除章节
chapter.get("/delchapter",async (req,res)=>{
    let {chapterid}=req.query
    let str=`delete from chapter where chapterid=${chapterid}`
    let data=await getMysql(str)
    res.send({status:200,message:"删除成功",data:data})
})

//根据章节id修改章节
chapter.post("/xiugaichapter",async (req,res)=>{
    let {articleid,chapterid,chaptertitle,chaptercontent}=req.body
    let str="update chapter set articleid=?,chaptertitle=?,chaptercontent=? where chapterid=?"
    let arr=[articleid,chaptertitle,chaptercontent,chapterid]
    let data=await getMysql(str,arr)
    res.send({status:200,message:"修改成功",data:data})
})


//提交新的章节
chapter.post("/newchapter",async (req,res)=>{
    let {articleid,chapterid,chaptertitle,chaptercontent,pushdate}=req.body
    let arr=[articleid,chaptertitle,chaptercontent]
    let str=`insert into chapter (articleid,chaptertitle,chaptercontent) values (?,?,?)`
    let data=await getMysql(str,arr)
    res.send({status:200,message:"提交新章节成功",data:data})
   
})

//获取当前文章的全部章节
chapter.get("/",async (req,res)=>{
    let {articleid,page,pageNumber}=req.query
    let str=`select * from chapter where articleid=${articleid} limit ?,?`
    let arr=[(page-1)*(pageNumber-0),pageNumber-0]
    let data=await getMysql(str,arr)
    let str1=`select count(1) from chapter`
    let tot=await getMysql(str1)
    // res.send(tot)
    res.send({status:200,tot:tot,message:"查找成功",data:data})  
})

module.exports = chapter

